#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

   //QMenuBar* menuBar = new QMenuBar(this);
    QMenuBar* menuBar = this->menuBar();
    //设置窗口有一个菜单栏
    this->setMenuBar(menuBar);

    //菜单
    QMenu* menu1= new QMenu("文件 (&F)");
    QMenu* menu2= new QMenu("编辑 (&E)");
    QMenu* menu3= new QMenu("视图 (&V)");
    QMenu* menu4= new QMenu("关于 (&A)");
    //菜单栏中加菜单
    menuBar->addMenu(menu1);
    menuBar->addMenu(menu2);
    menuBar->addMenu(menu3);
    menuBar->addMenu(menu4);

    //添加子菜单
    QMenu* menuChild = new QMenu("子菜单");
    menu1->addMenu(menuChild);
    //给子菜单添加菜单项
    QAction* actionChild = new QAction("菜单项");
    menuChild->addAction(actionChild);
    //设置菜单项
    QAction* action1 = new QAction("新建(&C)");
    action1->setIcon(":/create.jpg");
    QAction* action2 = new QAction("打开(&O)");
    QAction* action3 = new QAction("关闭(&Q)");
    menu1->addSeparator();
    menu1->addAction(action1);
    menu1->addSeparator();
    menu1->addAction(action2);
    menu1->addSeparator();
    menu1->addAction(action3);

    //菜单项的点击是一个信号，名为触发信号
    connect(action1,&QAction::triggered,this,&MainWindow::handle_create);
    connect(action2,&QAction::triggered,this,&MainWindow::handle_open);
    connect(action3,&QAction::triggered,this,&MainWindow::close);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handle_create()
{
    qDebug() << "新建";
}

void MainWindow::handle_open()
{
    qDebug() << "打开";
}

